package com.vcc.service.impl;


import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.vcc.core.util.DateUtil;
import com.vcc.core.util.RegexUtil;
import com.vcc.data.entity.Comptroller;
import com.vcc.data.mapper.ComptrollerMapper;
import com.vcc.service.service.ComptrollerService;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ComptrollerServiceImpl extends ServiceImpl<ComptrollerMapper, Comptroller> implements ComptrollerService {

    public Comptroller baseEntity(String ip, Long userId, Integer status, Integer useFlag) {
        Comptroller comptroller = new Comptroller();
        comptroller.setUserId(userId);
        comptroller.setUseFlag(useFlag);
        comptroller.setStatus(status);
        comptroller.setRequestIp(ip);
        comptroller.setRequestDate(new Date());
        return comptroller;
    }

    @Override
    public Comptroller register(String ip, Long userId, Integer status) {
        Comptroller comptroller = baseEntity(ip, userId, status, 0);
        save(comptroller);
        return comptroller;
    }

    @Override
    public Comptroller login(String ip, Long userId, Integer status) {
        Comptroller comptroller = baseEntity(ip, userId, status, 1);
        comptroller.setOperationUserId(userId);
        save(comptroller);
        return comptroller;
    }

    @Override
    public Comptroller block(String ip, Long userId, Integer status) {
        Comptroller comptroller = baseEntity(ip, userId, status, 2);
        save(comptroller);
        return comptroller;
    }

    @Override
    public Comptroller unBlock(String ip, Long userId, Integer status) {
        Comptroller comptroller = baseEntity(ip, userId, status, 3);
        save(comptroller);
        return comptroller;
    }

    @Override
    public Comptroller close(String ip, Long userId, Integer status) {
        Comptroller comptroller = baseEntity(ip, userId, status, 4);
        save(comptroller);
        return comptroller;
    }

    @Override
    public Comptroller updatePassword(String ip, Long userId, Integer status) {
        Comptroller comptroller = baseEntity(ip, userId, status, 5);
        save(comptroller);
        return comptroller;
    }

    @Override
    public Comptroller updatePayPassword(String ip, Long userId, Integer status) {
        Comptroller comptroller = baseEntity(ip, userId, status, 6);
        save(comptroller);
        return comptroller;
    }

    @Override
    public Page<Comptroller> paging(Page<Comptroller> pageable, Integer useFlag, Long userId, String startDate, String endDate) {
        QueryChain<Comptroller> queryChain = queryChain();
        queryChain.eq(Comptroller::getUserId, userId);
        if (RegexUtil.checkObjectIsNotNull(useFlag)) {
            queryChain.eq(Comptroller::getUseFlag, useFlag);
        }
        if (RegexUtil.checkObjectIsNotNull(startDate)) {
            queryChain.ge(Comptroller::getRequestDate, DateUtil.getDayBeginDate(startDate, "yyyy-MM-dd"));
        }
        if (RegexUtil.checkObjectIsNotNull(endDate)) {
            queryChain.le(Comptroller::getRequestDate, DateUtil.getDayEndDate(endDate, "yyyy-MM-dd"));
        }
        queryChain.orderBy(Comptroller::getRequestDate, false);
        return queryChain.page(pageable);
    }
}
